package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;

import com.shwread.android.qysw10000227.R;
import com.shwread.httpsdk.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class OMPayBalancePwdDialog
        extends
        Dialog
        implements
        android.view.View.OnKeyListener
{
	private Context            context;
	private DatePickerListener listener;
	private EditText           etPwd1, etPwd2, etPwd3, etPwd4, etPwd5, etPwd6, mEt;;
	private List<EditText>     list = new ArrayList<EditText>();
	private InputMethodManager inputManager;
	
	public OMPayBalancePwdDialog(Context context, DatePickerListener listener) {
		super(context, R.style.common_dialog);
		setCanceledOnTouchOutside(true);
		this.context = context;
		this.listener = listener;
		inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		findViews();
		setListener();
		getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.CENTER);
		getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(context).inflate(R.layout.om_pay_balance_password_dialog, null);
		setContentView(view);
		etPwd1 = (EditText) view.findViewById(R.id.et_om_pay_balance_password1);
		etPwd2 = (EditText) view.findViewById(R.id.et_om_pay_balance_password2);
		etPwd3 = (EditText) view.findViewById(R.id.et_om_pay_balance_password3);
		etPwd4 = (EditText) view.findViewById(R.id.et_om_pay_balance_password4);
		etPwd5 = (EditText) view.findViewById(R.id.et_om_pay_balance_password5);
		etPwd6 = (EditText) view.findViewById(R.id.et_om_pay_balance_password6);
		list.add(etPwd1);
		list.add(etPwd2);
		list.add(etPwd3);
		list.add(etPwd4);
		list.add(etPwd5);
		list.add(etPwd6);
		mEt = etPwd1;
		setFocusable();
	}
	
	private void setFocusable() {
		mEt.setFocusable(true);
		mEt.setFocusableInTouchMode(true);
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i) != mEt)
			{
				list.get(i).setFocusable(false);
				list.get(i).setFocusableInTouchMode(false);
			}
		}
	}
	
	private void setListener() {
		for (int i = 0; i < list.size() - 1; i++)
		{
			final EditText currEt = list.get(i);
			final EditText nextEt = list.get(i + 1);
			currEt.addTextChangedListener(new TextWatcher()
			{
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void afterTextChanged(Editable s) {
					if (Util.isNotEmpty(s.toString()))
					{
						currEt.clearFocus();
						mEt = nextEt;
						setFocusable();
						mEt.requestFocus();
					}
					
				}
			});
		}
		etPwd6.addTextChangedListener(new TextWatcher()
		{
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (Util.isNotEmpty(s.toString()))
				{
					StringBuffer buffer = new StringBuffer();
					for (int j = 0; j < list.size(); j++)
					{
						buffer.append(list.get(j).getText().toString());
					}
					String password = buffer.toString();
					listener.onSelect(password);
				}
			}
		});
		for (int i = 1; i < list.size(); i++)
		{
			list.get(i).setOnKeyListener(this);
		}
	}
	
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN)
		{
			for (int i = 0; i < list.size(); i++)
			{
				if (list.get(i) == mEt)
				{
					mEt.clearFocus();
					mEt = list.get(i - 1);
					mEt.setText("");
					setFocusable();
					mEt.requestFocus();
					break;
				}
			}
		}
		return false;
	}

	@Override
	public void show() {
		Timer timer = new Timer();
		timer.schedule(new TimerTask()
		{
			
			@Override
			public void run() {
				inputManager.showSoftInput(etPwd1, InputMethodManager.SHOW_IMPLICIT);
			}
		}, 100);
	    super.show();
	}
	
	public void reset() {
	    for (int i = 0; i < list.size(); i++)
	    {
	        list.get(i).setText("");
	        mEt.clearFocus();
	        mEt = etPwd1;
	        setFocusable();
	        mEt.requestFocus();
	    }
    }
	
}
